package com.oazon.moco.lan.core.http;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

import com.oazon.moco.lan.HttpJsonServerHandler;

public class HttpJsonServerService extends Service {

	private static String TAG = "HttpJsonServerService";
	public static final String PORT = "port";

	private int port = 9999;

	private HttpJsonServer server = HttpJsonServer
			.getInstance(HttpJsonServerHandler.class);

	@Override
	public IBinder onBind(Intent intent) {

		port = intent.getIntExtra("port", 9999);

		try {
			server.start(port);
		} catch (Exception e) {
			e.printStackTrace();
			Log.d(TAG, "HttpJsonServerService start failure");
		}
		return new HttpJsonServerServiceBinder();
	}

	public class HttpJsonServerServiceBinder extends Binder {

	}
	
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		if(intent != null){
			port = intent.getIntExtra("port", 55688);	
		}else{
			port = 55688;
		}
		try {
			server.start(port);
		} catch (Exception e) {
			e.printStackTrace();
			Log.d(TAG, "HttpJsonServerService start failure");
		}
		return super.onStartCommand(intent, flags, startId);
	}
	
	@Override
	public void onDestroy() {
		server.stop();
		super.onDestroy();
	}
}
